﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GMS.BIZ;

namespace Web.News
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!((Account)Session["Account"]).RoleCurrent.Equals('a')) Response.Redirect("Default.aspx");

            refreshgrvnews();

            #region ViewState

            //初始化ViewState       
            if (ViewState["status"] == null)
                ViewState["status"] = 1;

            this.ViewStateControls(int.Parse(ViewState["status"].ToString()));

            #endregion ViewState
        
        }

        protected void refreshgrvnews()
        {
            GMS.BIZ.News myNews = new GMS.BIZ.News();
            DataSet ds = myNews.GetList("1=1 ORDER BY news_date desc");
            if (ds.Tables[0].Rows.Count != 0)
            
                GridView_News.DataSource = ds;
                GridView_News.DataBind();
            
        }

        //GridView查看按钮
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton newImageButton = (ImageButton)sender;
            Response.Redirect("GongGaoDetail.aspx?id=" + newImageButton.CommandArgument);
        }

       
        //设置ViewState对应的添加、修改、删除三种状态
        protected void ViewStateControls(int i)
        {
            if (i == 1)//添加
            {
                ImageButton_Cancel.Visible = false;
                ImageButton_Add.Visible = true;
                ImageButton_Edit.Visible = false;
                ImageButton_Delete.Visible = false;
            }
            else if (i == 2)//修改
            {
                ImageButton_Cancel.Visible = true;
                ImageButton_Add.Visible = false;
                ImageButton_Edit.Visible = true;
                ImageButton_Delete.Visible = false;
            }
            else if (i == 3) //删除
            {
                ImageButton_Cancel.Visible = true;
                ImageButton_Add.Visible = false;
                ImageButton_Edit.Visible = false;
                ImageButton_Delete.Visible = true; 
            }
        }

        //GridView修改按钮
        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            GMS.BIZ.News myNews = new GMS.BIZ.News();
            myNews=myNews.GetModel(decimal.Parse(((ImageButton)sender).CommandArgument));
            TextBox_newstitle.Text=myNews.news_title.ToString();
            myFCKeditor.Value =myNews.news_content.ToString();
            Label_newsid.Text = myNews.news_id.ToString(); 

            ViewState["status"] = 2;
            Label_result.Text = "";
        }
        //Gridview删除按钮
        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            GMS.BIZ.News myNews = new GMS.BIZ.News();
            myNews = myNews.GetModel(decimal.Parse(((ImageButton)sender).CommandArgument));
            TextBox_newstitle.Text = myNews.news_title.ToString();
            myFCKeditor.Value = myNews.news_content.ToString();
            Label_newsid.Text = myNews.news_id.ToString();

            ViewState["status"] = 3;
            Label_result.Text = "";
        }
        // 修改按钮
        protected void ImageButton_Edit_Click(object sender, ImageClickEventArgs e)
        {
            if (TextBox_newstitle.Text == string.Empty || myFCKeditor.Value == string.Empty) Label_result.Text = "请填写标题或正文内容";
            else
            {
                GMS.BIZ.News myNews = new GMS.BIZ.News();
                myNews.news_id = decimal.Parse(Label_newsid.Text.ToString());
                myNews.news_title = TextBox_newstitle.Text.ToString();
                myNews.news_content = myFCKeditor.Value;
                myNews.news_date = DateTime.Now;
                bool ok = myNews.Update(myNews);
                if (!ok) Label_result.Text = Resources.ErrorMessage.DBOptError;
                else Label_result.Text = Resources.ErrorMessage.OK;

                ViewState["status"] = 1;
                TextBox_newstitle.Text = null;
                myFCKeditor.Value = null;
            }

        }
        //添加按钮
        protected void ImageButton_Add_Click(object sender, ImageClickEventArgs e)
        {
            if (TextBox_newstitle.Text == string.Empty || myFCKeditor.Value == string.Empty) Label_result.Text = "请填写标题或正文内容";
            else
            {
                GMS.BIZ.News myNews = new GMS.BIZ.News();
                myNews.news_title = TextBox_newstitle.Text.ToString();
                myNews.news_content = myFCKeditor.Value;
                myNews.news_date = DateTime.Now;
                bool ok = myNews.Add(myNews);
                if (!ok) Label_result.Text = Resources.ErrorMessage.DBOptError;
                else Label_result.Text = Resources.ErrorMessage.OK;

                ViewState["status"] = 1;
                TextBox_newstitle.Text = null;
                myFCKeditor.Value = null;
            }
        }
        //删除按钮
        protected void ImageButton_Delete_Click(object sender, ImageClickEventArgs e)
        {
            GMS.BIZ.News myNews = new GMS.BIZ.News();
            bool ok=myNews.Delete(decimal.Parse(Label_newsid.Text.ToString()));
            if(!ok)
            Label_result.Text=Resources.ErrorMessage.DBOptError;
            else Label_result.Text=Resources.ErrorMessage.OK;
            
            ViewState["status"]=1;
            TextBox_newstitle.Text = null;
            myFCKeditor.Value = null;
        }
        //取消按钮
        protected void ImageButton_Cancel_Click(object sender, ImageClickEventArgs e)
        {
            TextBox_newstitle.Text = null;
            myFCKeditor.Value = null;

            ViewState["status"] = 1;
        }

        protected void GridView_News_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_News.PageIndex = e.NewPageIndex;
            refreshgrvnews();
        }
    }
}
